package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.enterprise.models.SpecialProtectionProject;
import com.budwk.app.enterprise.services.SpecialProtectionProjectService;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@IocBean
@At("/platform/enterprise/protection/special")
@ApiDefinition(tag = "企业环保专项")
@Slf4j
public class SpecialProtectionProjectController {
    @Inject
    private SpecialProtectionProjectService specialProtectionProjectService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询企业环保专项")
    @ApiFormParams(implementation = SpecialProtectionProject.class)
    @ApiResponses(implementation = SpecialProtectionProject.class)
    @SaCheckPermission("special.protection.project.listByPage")
    public Result<?> list(@Param("..")SpecialProtectionProject bean) {
        log.info("SpecialProtectionProjectController listByPage param: {}", JSONObject.toJSONString(bean));
        return Result.data(specialProtectionProjectService.listByPage(bean));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增企业环保专项")
    @ApiFormParams(
        implementation = SpecialProtectionProject.class
    )
    @ApiResponses
    @SLog(tag = "新增企业环保专项", value = "${args[0].id}")
    @SaCheckPermission("special.protection.project.create")
    public Result<?> create(@Param("..")SpecialProtectionProject dfxyEnterprise) {
        dfxyEnterprise.setCreatedBy(SecurityUtil.getUserId()).setCreatedAt(LocalDateTime.now()).setUpdatedAt(LocalDateTime.now()).setUpdatedBy(SecurityUtil.getUserId());
        specialProtectionProjectService.insert(dfxyEnterprise);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改企业环保专项")
    @ApiFormParams(
        implementation = SpecialProtectionProject.class
    )
    @ApiResponses
    @SLog(tag = "修改企业环保专项", value = "${args[0].id}")
    @SaCheckPermission("special.protection.project.update")
    public Result<?> update(@Param("..")SpecialProtectionProject dfxyEnterprise, HttpServletRequest req) {
        dfxyEnterprise.setUpdatedBy(SecurityUtil.getUserId()).setUpdatedAt(LocalDateTime.now());
        specialProtectionProjectService.updateIgnoreNull(dfxyEnterprise);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取企业环保专项")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("special.protection.project.getData")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        SpecialProtectionProject dfxyEnterprise = specialProtectionProjectService.fetch(id);
        if (dfxyEnterprise == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(dfxyEnterprise);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除企业环保专项")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除企业环保专项", value = "${args[0]}")
    @SaCheckPermission("special.protection.project.delete")
    public Result<?> delete(@Param("id")String id) {
        log.info("SpecialProtectionProjectController delete userId: {}, id: {}", SecurityUtil.getUserId(), id);
        SpecialProtectionProject dfxyEnterprise = specialProtectionProjectService.fetch(id);
        if (dfxyEnterprise == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        specialProtectionProjectService.delete(id);
        return Result.success();
    }
}
